package Bidostar_Page;

import Bidostar_BaseData.Page;
import Bidostar_Data.Baseinfo;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class MobileEyePage extends Page
{
    public MobileEyePage(WebDriver driver)
    {
        super(driver);
    }

    public void clickMobileEyePage()
    {
        getElement("MobileEyePage").click();
    }

    public void ChooseData()
    {
        driver.findElement(By.xpath("html/body/div[1]/div[3]/div/div[1]/div[2]/label")).click();
    }

    public void ChooseDataByNow()
    {
        driver.findElement(By.id("dpTodayInput")).click();
    }

    public void clickAddressChoose(String AddressChoose)
    {
        getElement("AddressChoose").clear();
        getElement("AddressChoose").sendKeys(AddressChoose);
    }

    public void clickSearch_Btn()
    {
        getElement("Search_Btn").click();
    }

    public void clickStratTrace()
    {
        getElement("StratTrace").click();
    }

    public boolean MobileEyePageTest() throws Exception
    {
        try
        {
            clickMobileEyePage();
            Thread.sleep(200);
//            ChooseData();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            JavascriptExecutor removeAttribute = (JavascriptExecutor) driver;

            Date date = new Date();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


            removeAttribute.executeScript("arguments[0].removeAttribute(\"readonly\") ", driver.findElement(By.id("datetimepicker")));

            driver.findElement(By.xpath("//input[@id='datetimepicker']")).sendKeys(sdf.format(date));

            try
            {
                if (driver.getCurrentUrl().contains("ta.bidostar.com"))
                {

                    Assert.assertEquals(driver.getTitle(), Baseinfo.Ta_title);

                    logger.info("验证成功");

                } else if (driver.getCurrentUrl().contains("ci.bidostar.com"))
                {
                    logger.warn(Baseinfo.Ci_title + "暂无此功能");
                } else if (driver.getCurrentUrl().contains("dc.bidostar.com"))
                {
                    logger.warn(Baseinfo.Dc_title + "暂无此功能");
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }

            clickAddressChoose(Baseinfo.Address);
            clickSearch_Btn();
            Thread.sleep(200);
            clickStratTrace();

            if(waitForWebElement(By.id("loding_prompt"))){
                logger.warn(getElement("Nocar").getText());
            }
            if (getElement("Nocar").isDisplayed())
            {
                logger.warn(getElement("Nocar").getText());
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }
}
